<?php
if ( defined('ABSPATH') )
	include_once ABSPATH . 'config.php';
else
    include_once '../config.php';

include_once ABSPATH."common/date.php";    
    
    
class Post {
  var $status = '';
  var $source = '';

function Update($id,$channel, $status, $link, $tags){
    global $db;
    $status = $status;
    $link = $link;
    $tags = $tags;
    
    $result = $db->query("insert into $db->posts (ownerid, channel_id, status, link, tags, timestamp) values($id,'".$channel."','".$db->escape($status)."','".$link."','".$db->escape($tags)."',Now())");
    
    $permalink = 'http://'.$_SERVER["HTTP_HOST"] . $_SERVER["PATH_INFO"] ."users/?id=" . $id . '&p='. $db->insert_id;
    
    $result = $db->query("update $db->posts set permalink='$permalink' where id='$db->insert_id'");
    
}
function Get($id, $contact_id='', $last_update = '1900-01-01', $num_records=20){
    global $db;
    
    if(!isValidDateTime($last_update))
    {
    	$last_update = "1900-01-01";
    }
    
    if(strlen($contact_id) > 0)
    {
	    $query = "select A.id,A.timestamp,A.status,A.source,A.link, A.permalink from $db->posts A, $db->connections B, $db->cnxn_channels C "
    	 	."where A.ownerid=B.ownerid and B.contact_id ='$contact_id' and B.id=C.cnxn_id and A.channel_id=C.channel_id and A.ownerid='$id' and timestamp > '$last_update' order by timestamp DESC limit $num_records";
    }
    else
    {
	    $query = "select A.id,A.timestamp,A.status,A.source,A.link, A.permalink from $db->posts A "
    	 	."where A.ownerid='$id' and A.channel_id='1' and timestamp > '$last_update' order by timestamp DESC limit $num_records";    	
    }
    
    $rs = $db->get_results($query, ARRAY_A);
    return $rs;
}

function ConnectionsGet($id, $channel=1, $offset=0, $rowsPerPage=20){
    global $db;
    $query = "select C.name,C.screen_name,C.image,A.status,A.link,A.tags,A.timestamp,A.source from $db->posts A, $db->connections B, $db->people C, $db->cnxn_channels D where A.ownerid=B.contact_id and B.contact_id=C.id and B.id=D.cnxn_id and B.ownerid='$id' and A.channel_id='$channel' and A.channel_id=D.channel_id order by A.timestamp DESC limit $offset,$rowsPerPage";
    $rs = $db->get_results($query, ARRAY_A);
    return $rs;
}

function ConnectionsGetTotalCount($id, $channel=1){
    global $db;
    $cnt = $db->get_var("select count(A.id) `cnt` from $db->posts A, $db->connections B, $db->people C, $db->cnxn_channels D where A.ownerid=B.contact_id and B.contact_id=C.id and B.id=D.cnxn_id and B.ownerid='$id' and A.channel_id='$channel' and A.channel_id=D.channel_id");
    return $cnt ;
}


}

?>